
Public Class ColumnDefinition
    Implements IAlias

    Public ColumnName As New ColumnName
    Public Aggregate As String
    Private _Alias

    Public Property ColumnAlias() As String Implements IAlias.Alias
        Get
            Return _Alias
        End Get
        Set(ByVal value As String)
            _Alias = value
        End Set
    End Property

    Public Property Name() As String Implements IAlias.Name
        Get
            Return ColumnName.Name
        End Get
        Set(ByVal value As String)
            ColumnName.Name = value
        End Set
    End Property

    Public Function GetName() As String
        If ColumnAlias = String.Empty Then
            Return ColumnName.Name
        Else
            Return ColumnAlias
        End If
    End Function
    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub
    Public Sub New(ByVal name As String, ByVal [alias] As String)
        Me.Name = name
        Me.ColumnAlias = [alias]
    End Sub

    Public Sub New()

    End Sub
End Class

Public Class ColumnList
    Inherits AliasList(Of ColumnDefinition)

    Private _IsWildcard As Boolean
    Public Property IsWildCard() As Boolean
        Get
            Return _IsWildcard
        End Get
        Set(ByVal value As Boolean)
            _IsWildcard = value
        End Set
    End Property
    Public Function GetColumnsByTable(ByVal tables As Generic.List(Of Table), ByVal TableName As String) As ColumnList
        'ToDo:
        'This function should return filter the list of columns that exist in TableName.
        Dim list As New ColumnList
        For Each table As Table In tables
            If table.List.ToUpper() = TableName.ToUpper OrElse table.Alias.ToUpper() = TableName.ToUpper() Then
                For Each column As ColumnDefinition In Me
                    If column.ColumnName.TableAlias = String.Empty Then
                        'Select statement didn't have a table alias
                        list.Add(column)
                    Else
                        If column.ColumnName.TableAlias.ToUpper = table.List.ToUpper OrElse column.ColumnName.TableAlias.ToUpper = table.Alias.ToUpper() Then
                            list.Add(column)
                        End If
                    End If
                Next
            End If
        Next
        Return list
    End Function

End Class

Public Class ColumnName
    Public TableAlias As String
    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            If value.Contains(".") Then
                Me.TableAlias = value.Split(".")(0)
                Me._Name = value.Split(".")(1)
            Else
                Me._Name = value
            End If
        End Set
    End Property
    Public Function GetFullName() As String
        If TableAlias = String.Empty Then
            Return Name
        Else
            Return TableAlias & "." & Name
        End If
    End Function
End Class